package xyz.fefine;

import java.util.List;

/**
 * Created by feng_ on 2016/9/11.
 */
public class ForStmnt extends ASTList {
    public ForStmnt(List<ASTree> list) {
        super(list);
    }

    public ASTree init(){
        return child(0);
    }

    public ASTree condition(){
        return child(1);
    }

    public ASTree execute(){
        return child(2);
    }

    public BlockStmnt body(){
        return (BlockStmnt)child(3);
    }

    @Override
    public Object eval(Enviorment enviorment) {
        Object res = null;
        //初始化
        init().eval(enviorment);
        //判断
        Object con = condition().eval(enviorment);
        //执行
        while (con instanceof Integer && (Integer) con == TRUE) {
            res = body().eval(enviorment);
            //继执行
            execute().eval(enviorment);
            //判断
            con = condition().eval(enviorment);
        }
        return res;
    }

    @Override
    public String toString() {

        return "( for ("+init()+";"+condition()+";"+execute()+")"+body()+")";
    }
}
